<?php
/*Mostafa Shalkami (C) 2010 CC Share-Alike 3.0 Some Rights Reserved
*shalkamm@gmail.com
*
*This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License.
*To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a 
*letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
*
*/
class sobi_nestedsearch {

/*
 * ************************************************************
 * required methods
 * ************************************************************
 */
	
	var $name = "nestedsearch";
	var $listingStyle = "sobi_nestedsearch_VC";
	var $user;
  var $config;
  var $fid;
  var $secondary;
  var $fields= array();
	
function sobi_hello() {
	error_reporting(E_NOTICE | E_ERROR);
	$this->config =& sobi2Config::getInstance();
	//   $database =& $config->getDb();
    $this->secondary = $this->config->getValueFromDB("sobi_nestedsearch","s_fields");
	 $this->secondary = explode(";", $this->secondary);
   foreach ($this->secondary as $field){
     $fc = explode(":", $field);
     $this->fields = $this->fields + array($fc[0]=>$fc[1]);
   }
	}

function showDetails($sobi2Id) {
 	  	
return NULL;
    }
function save($input,$sobi2Id) {

}


function update($input,$sobi2Id){

}
function onAjaxSearchStart($searchString, $phrase, &$dropListsArray, $cookieValues, $fieldsNames, $autoSearch) {
ob_start();
?>
 <script type='text/javascript'>
    search_button = document.getElementById("sobiSearchFormExtOptToggle");
<?php
foreach ($this->fields as $key=>$value){
if ($key) {
  $value = explode(",", $value);
  $indexes = $this->genFields($key); 
?>
   obj_<?php echo $key; ?> = document.getElementById("<?php echo $key; ?>");
  var fields_<?php echo $key; ?> = new Array;
    <?php 
    
    for($i=0;$i<count($indexes); $i++){
      
      if($value[$i]) echo "fields_{$key}[$i]=document.getElementById(\"".$value[$i]."\");\n";
    }     
   ?>
  search_button.onclick = HideAll(fields_<?php echo $key; ?>);
obj_<?php echo $key; ?>.onchange = function (){
  <?php foreach($indexes as $index) { 
  if($value[$index->description-1]) {
  ?>
  if(obj_<?php echo $key; ?>.selectedIndex == <?php echo $index->description; ?>) {
    HideAll(fields_<?php echo $key; ?>);
    var x = document.getElementById('<?php echo $value[$index->description-1]; ?>');
    showOne(x);
   } 
   <?php
  }
    } ?>
  };
<?php
}
 } ?>

  function HideAll(vThis)
{
 for (var i = 0; i < vThis.length; i++) {
vThis[i].selectedIndex = 0;
 vParent = vThis[i].parentNode;
  vParent2 = vParent.parentNode;
    vParent2.style.display = 'none';
      } 
return;
}
    function showOne(field) {
  vParent = field.parentNode;
  vParent2 = vParent.parentNode;
  vParent2.style.display = 'table-row';
     } 
  </script>
<?php
// save catched output in variable
$str = ob_get_contents();
// stop catching output 
ob_end_clean();
//$this->config->addCustomHeadTag($str);
$dropListsArray = $dropListsArray + array("<label id='field_null'><label/>"=>"<div id='field_null'>{$str}</div>"); 

}
function genFields($fid){
 $db =& JFactory::getDBO();
 $query = "SELECT  `fieldid` FROM  `#__sobi2_language` WHERE  `langKey` ='{$fid}'";
$db->setQuery($query);
$id = $db->loadResult();
$query = "SELECT  `description` FROM  `#__sobi2_language` WHERE  `fieldid` ={$id} AND  `sobi2Section` =  'field_opt'";
$db->setQuery($query);
$result = $db->loadObjectList();
return $result;
}
 }
?>


